package com.bear.atlassianmcp.oauth.model;

import com.bear.atlassianmcp.oauth.strategy.AuthenticationStrategy.ServiceType;

import java.util.HashMap;
import java.util.Map;

/**
 * OAuth 请求上下文
 * 用于在 OAuth 签名生成过程中传递请求相关信息
 */
public class OAuthRequestContext {
    
    /**
     * 服务类型（Jira 或 Confluence）
     */
    private ServiceType serviceType;
    
    /**
     * 完整的请求 URL
     */
    private String requestUrl;
    
    /**
     * HTTP 方法
     */
    private String httpMethod;
    
    /**
     * 额外的请求参数
     */
    private Map<String, String> additionalParams;
    
    /**
     * 请求体内容（用于 POST/PUT 请求）
     */
    private String requestBody;
    
    /**
     * 是否为测试连接请求
     */
    private boolean testConnection;
    
    // 构造函数
    public OAuthRequestContext() {
        this.additionalParams = new HashMap<>();
        this.httpMethod = "GET"; // 默认为 GET
        this.testConnection = false;
    }
    
    public OAuthRequestContext(ServiceType serviceType, String requestUrl, String httpMethod) {
        this();
        this.serviceType = serviceType;
        this.requestUrl = requestUrl;
        this.httpMethod = httpMethod;
    }
    
    // Builder 模式
    public static Builder builder() {
        return new Builder();
    }
    
    public static class Builder {
        private final OAuthRequestContext context;
        
        public Builder() {
            this.context = new OAuthRequestContext();
        }
        
        public Builder serviceType(ServiceType serviceType) {
            context.serviceType = serviceType;
            return this;
        }
        
        public Builder requestUrl(String requestUrl) {
            context.requestUrl = requestUrl;
            return this;
        }
        
        public Builder httpMethod(String httpMethod) {
            context.httpMethod = httpMethod;
            return this;
        }
        
        public Builder additionalParam(String key, String value) {
            context.additionalParams.put(key, value);
            return this;
        }

        public Builder addParam(String key, String value) {
            context.additionalParams.put(key, value);
            return this;
        }

        public Builder additionalParams(Map<String, String> params) {
            if (params != null) {
                context.additionalParams.putAll(params);
            }
            return this;
        }
        
        public Builder requestBody(String requestBody) {
            context.requestBody = requestBody;
            return this;
        }
        
        public Builder testConnection(boolean testConnection) {
            context.testConnection = testConnection;
            return this;
        }
        
        public OAuthRequestContext build() {
            return context;
        }
    }
    
    // Getters and Setters
    public ServiceType getServiceType() {
        return serviceType;
    }
    
    public void setServiceType(ServiceType serviceType) {
        this.serviceType = serviceType;
    }
    
    public String getRequestUrl() {
        return requestUrl;
    }
    
    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }
    
    public String getHttpMethod() {
        return httpMethod;
    }
    
    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }
    
    public Map<String, String> getAdditionalParams() {
        return additionalParams;
    }
    
    public void setAdditionalParams(Map<String, String> additionalParams) {
        this.additionalParams = additionalParams != null ? additionalParams : new HashMap<>();
    }
    
    public String getRequestBody() {
        return requestBody;
    }
    
    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }
    
    public boolean isTestConnection() {
        return testConnection;
    }
    
    public void setTestConnection(boolean testConnection) {
        this.testConnection = testConnection;
    }
    
    /**
     * 添加单个参数
     */
    public void addParam(String key, String value) {
        this.additionalParams.put(key, value);
    }
    
    /**
     * 获取单个参数
     */
    public String getParam(String key) {
        return this.additionalParams.get(key);
    }
    
    /**
     * 检查是否包含指定参数
     */
    public boolean hasParam(String key) {
        return this.additionalParams.containsKey(key);
    }
    
    /**
     * 获取基础 URL（不包含查询参数）
     */
    public String getBaseUrl() {
        if (requestUrl == null) {
            return null;
        }
        
        int queryIndex = requestUrl.indexOf('?');
        if (queryIndex != -1) {
            return requestUrl.substring(0, queryIndex);
        }
        
        return requestUrl;
    }
    
    /**
     * 从 URL 中提取查询参数
     */
    public Map<String, String> extractQueryParams() {
        Map<String, String> queryParams = new HashMap<>();
        
        if (requestUrl == null) {
            return queryParams;
        }
        
        int queryIndex = requestUrl.indexOf('?');
        if (queryIndex == -1) {
            return queryParams;
        }
        
        String queryString = requestUrl.substring(queryIndex + 1);
        String[] pairs = queryString.split("&");
        
        for (String pair : pairs) {
            String[] keyValue = pair.split("=", 2);
            if (keyValue.length == 2) {
                queryParams.put(keyValue[0], keyValue[1]);
            }
        }
        
        return queryParams;
    }
    
    /**
     * 合并所有参数（查询参数 + 额外参数）
     */
    public Map<String, String> getAllParams() {
        Map<String, String> allParams = new HashMap<>();
        allParams.putAll(extractQueryParams());
        allParams.putAll(additionalParams);
        return allParams;
    }
    
    @Override
    public String toString() {
        return "OAuthRequestContext{" +
               "serviceType=" + serviceType +
               ", requestUrl='" + requestUrl + '\'' +
               ", httpMethod='" + httpMethod + '\'' +
               ", additionalParams=" + additionalParams +
               ", requestBody='" + (requestBody != null ? "[" + requestBody.length() + " chars]" : "null") + '\'' +
               ", testConnection=" + testConnection +
               '}';
    }
}
